<?php

class Tours extends Controller {

	function Tours()
	{
		parent::Controller();
		$this->load->helper(array('url', 'date', 'boolean'));	
		$this->load->library('session');
	}
	
	function index()
	{
		$this->load->helper('tourtypes');	
		$this->load->model('Tour_model', '', TRUE);
		$this->load->model('Tourtype_model', '', TRUE);
		
		
		
		$data = array();
		
		$data['tour_types'] = $this->Tourtype_model->get_all_tourtypes();		
		//$data['tours'] = $this->Tour_model->get_all_tours();
		
		if (isset($_POST['search']))
		{
			$this->load->model('Search_model', '', TRUE);
			$data['results'] = $this->Search_model->tours($_POST);	
		}

		
		$this->load->view('tours/index', $data);
	}
	
	function add()
	{
		$this->load->helper('tourtypes');	
		$this->load->model('Tourtype_model', '', TRUE);
		
		
		
		$data = array();
		
		$data['tours'] = $this->Tourtype_model->get_all_tourtypes();
		
		
		$this->load->view('tours/add', $data);
	}
	
	function create()
	{
		
		$this->load->model('Tour_model', '', TRUE);
		
		$id = $this->Tour_model->add_tour();
		
		redirect(site_url("tours/edit/$id"));
	}
	
	function show($id)
	{
		$this->load->helper(array('tourtypes', 'bus', 'hostels'));
		$this->load->model('Tour_model', '', TRUE);
		$this->load->model('Tourtype_model', '', TRUE);
		$this->load->model('Itinerary_model', '', TRUE);
		$this->load->model('Bus_model', '', TRUE);
		
		$data = array();
		
		$data['tour'] = $this->Tour_model->get_tour($id);
		$data['tourtypes'] = $this->Tourtype_model->get_all_tourtypes();
			
		$data['bus_list'] = $this->Bus_model->get_all_buses();
		$data['default_itinerary'] = $this->Itinerary_model->get_itinerary($data['tour']['tourtype_id']);
			
		$data['tour_buses'] = $this->Bus_model->get_tour_buses($id);
		$data['custom_itineraries'] = array();
			
		if (count($data['tour_buses']) > 0)
		{
			for ($i = 0; $i < count($data['tour_buses']); $i++)
			{
				if (!$data['tour_buses'][$i]['default_itinerary'])
				{
					$data['custom_itineraries'][$data['tour_buses'][$i]['bus_number']] = $this->Itinerary_model->get_itinerary(null, $data['tour']['id'], $data['tour_buses'][$i]['bus_number']);
				}
			}
		} else
		{
			$data['tour_buses'][0] = array(
											'bus_id' => '0',
											'bus_number' => '1',
											'default_itinerary' => '1'
											);
		}
			
		$this->load->view('tours/show', $data);
	}
	
	function edit($id)
	{
		$this->load->helper(array('tourtypes', 'bus', 'hostels'));
		$this->load->model('Tour_model', '', TRUE);
		$this->load->model('Tourtype_model', '', TRUE);
		$this->load->model('Itinerary_model', '', TRUE);
		$this->load->model('Bus_model', '', TRUE);
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('tourtype', 'Tour Type', 'required');
		
		if ($this->form_validation->run() == FALSE)
		{
			
			
			$data = array();
		
			$data['tour'] = $this->Tour_model->get_tour($id);
			$data['tourtypes'] = $this->Tourtype_model->get_all_tourtypes();
			
			$data['bus_list'] = $this->Bus_model->get_all_buses();
			$data['default_itinerary'] = $this->Itinerary_model->get_itinerary($data['tour']['tourtype_id']);
			
			$data['tour_buses'] = $this->Bus_model->get_tour_buses($id);
			$data['custom_itineraries'] = array();
			
			if (count($data['tour_buses']) > 0)
			{
				for ($i = 0; $i < count($data['tour_buses']); $i++)
				{
					if (!$data['tour_buses'][$i]['default_itinerary'])
					{
						$data['custom_itineraries'][$data['tour_buses'][$i]['bus_number']] = $this->Itinerary_model->get_itinerary(null, $data['tour']['id'], $data['tour_buses'][$i]['bus_number']);
					}
				}
			} else
			{
				$data['tour_buses'][0] = array(
												'bus_id' => '0',
												'bus_number' => '1',
												'default_itinerary' => '1'
												);
			}
			
			$this->load->view('tours/edit', $data);
		} else
		{
			//echo '<pre>';
			//print_r($_POST);
			//echo '</pre>';
			
			if (isset($_POST['save']) || isset($_POST['saveAndActivate']))
			{
				$this->Tour_model->update($id);
				
				$this->Bus_model->clear_tour_buses($id);
				
				for ($i = 0; $i < count($_POST['buses']); $i++)
				{
					$this->Bus_model->update_tour_buses($id, $_POST['buses'][$i]);
					
					if (!isset($_POST['buses'][$i]['default_itinerary']))
					{
						$this->Itinerary_model->create_or_update($_POST['buses'][$i]['hostel_names'], null, $id, $_POST['buses'][$i]['bus_number']);	
					} else
					{
						$this->Itinerary_model->delete_itinerary(null, $id, $_POST['buses'][$i]['bus_number']);
					}
				}
				
				$this->Tour_model->updateCapacity($id);
			}
			
			if(isset($_POST['activate']) || isset($_POST['saveAndActivate']))
			{
				// Check bus data
				if ($this->Tour_model->check_buses($id))
				{				
					$this->Tour_model->activate($id);
				} else
				{
					$this->session->set_flashdata('msg', 'Assign buses before activation!');
				}
			}
			
			if(isset($_POST['deactivate']))
			{
				$this->Tour_model->deactivate($id);
			}
			
			redirect(site_url("tours/edit/$id"));
		}
	}
	
	function additionalBus($count, $tour_type)
	{
		$this->load->helper(array('bus', 'hostels'));
		$this->load->model('Itinerary_model', '', TRUE);
		$this->load->model('Bus_model', '', TRUE);
		
		$data = array();
		$data['count'] = $count;
		$data['tour_type'] = $tour_type;
		
		$data['bus_list'] = $this->Bus_model->get_all_buses();
		$data['default_itinerary'] = $this->Itinerary_model->get_itinerary($tour_type);
		
		$this->load->view('tours/additionalBus', $data);
	}
	
	function calendar()
	{
		$prefs = array();
		$prefs['template'] = '{table_open}<table cellpadding="0" cellspacing="0" class="calTable">{/table_open},  					                              {week_day_cell}<td class="dayHeader">{week_day}</td>{/week_day_cell}';
		$prefs['start_day'] = 'monday';
		
		$this->load->library('calendar', $prefs);
		$this->load->model('Tour_model', '', TRUE);

		$tours = $this->Tour_model->get_tours_by_month(6, 2009);
		
		$tour_array = $this->calendar->calendar_array($tours, 6, 2009);
		
		
		$data = array();
		$data['cal'] = $this->calendar->advanced_generate(2009, 6, $tour_array);
		
		
		



		$this->load->view('tours/calendar', $data);	
	}

}
?>